package com.zxmr.hospital.info.action;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.domain.account.AccountStatus;
import com.zxmr.domain.account.Role;
import com.zxmr.domain.account.User;
import com.zxmr.domain.service.UserService;
import com.zxmr.hospital.info.action.dto.JSONListDTO;
import com.zxmr.navigate.Executor;

public class QueryAllEmployee extends Executor {

	class queryAllEmployeeDTO{
		int id;
		String name;
		String display;
		String cell;
		String email;
		String roles;
		AccountStatus status;
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getDisplay() {
			return display;
		}
		public void setDisplay(String display) {
			this.display = display;
		}
		public String getCell() {
			return cell;
		}
		public void setCell(String cell) {
			this.cell = cell;
		}
		public String getEmail() {
			return email;
		}
		public void setEmail(String email) {
			this.email = email;
		}
		public String getRoles() {
			return roles;
		}
		public void setRoles(String roles) {
			this.roles = roles;
		}
		public AccountStatus getStatus() {
			return status;
		}
		public void setStatus(AccountStatus status) {
			this.status = status;
		}
		
		
	}
	@Override
	public void execute(Request request, Response response) {
		// TODO Auto-generated method stub
		
		UserService service = new UserService();
		User requestUser = (User)request.getAccount();
		List<User> es = service.findAllUserByCompany(requestUser.getCompany());
		List<queryAllEmployeeDTO> dtos = transferUserToDTO(es);
		
		JSONListDTO users = new JSONListDTO();
		users.setRows(dtos);
		users.setTotal(dtos.size());
		ResponsePayload payload = new ResponsePayload();
		
		payload.setValue(JSON.toJSONString(users));
		response.setPayload(payload);
	}
	
	private List<queryAllEmployeeDTO> transferUserToDTO(List<User> users)
	{
		List<queryAllEmployeeDTO> dtos = new ArrayList<queryAllEmployeeDTO>();
		
		if(users == null)return dtos;
		
		UserService service = new UserService();
		for(User user : users)
		{
			queryAllEmployeeDTO dto = new queryAllEmployeeDTO();
			dto.setId(user.getId());
			dto.setCell(user.getCell());
			dto.setDisplay(user.getDisplay());
			dto.setEmail(user.getEmail());
			dto.setName(user.getName());
			dto.setStatus(user.getStatus());
			List<Role> roles = service.getAllRole(user);
			String role = "";
			for(Role r : roles)
			{
				role+=r.getName()+" ";
			}
			dto.setRoles(role);
			dtos.add(dto);
		}
		return dtos;
	}

}
